# -*- coding: utf-8 -*-
from datetime import datetime, date
from django.core.paginator import Paginator, EmptyPage
from django.conf import settings
from django.http import Http404
from apps.comments.models import Comment, CommentNotification
from core.decorators import render
from core.models import Category
from ..models import Record

@render('news/full_story.html')
def record(request, url, pk, record):

    #page_unique_id = Comment.calculate_url(request.META.get('PATH_INFO'))

    try:
        record = Record.objects.get(
            category__url = url,
            url = record,
            pk = pk
        )

        # remove CommentNotifications ...
        try:
            CommentNotification.objects.filter(
                record = record,
                user = request.user
            ).delete()

        except Exception, e:
            pass

    except Exception, e:
        raise Http404

    # увеличим кол-во просмотров
    record.views += 1
    record.save()

    return {
        'record': record#,
        #'page_unique_id': page_unique_id
    }

@render('news/category.html')
def calendar(request, year, month=None, day=None, page=1):

    categories_id = Category.objects.all().values_list('pk', flat=True)

    records_list = Record.objects.select_related().filter(
        status = Record.STATUS_PUBLISHED,
        publish_date__lte = datetime.now(),
        publish_date__year = year,
        category__in = categories_id
    ).order_by('-publish_date')

    if month:
        records_list = records_list.filter(
            publish_date__month = month
        )

    if day:
        records_list = records_list.filter(
            publish_date__day = day
        )

    paginator = Paginator(records_list, settings.LIVE_ENGINE_NEWS_PER_PAGE)

    try:
        records = paginator.page(page)
    except EmptyPage:
        raise Http404

    path = "/%s/" % year
    if month: path += "%s/" % month
    if day: path += "%s/" % day

    return {
        'records': records,
        'pagination_rule': '%spage/1/' % path,
        'date': path.replace("/", " ")
    }

@render('news/category.html')
def category(request, url, page=1):

    try:
        category = Category.objects.get(url = url)
        
    except Category.DoesNotExist:
        raise Http404

    records_list = Record.objects.select_related().filter(
        category = category,
        status = Record.STATUS_PUBLISHED,
        publish_date__lte = datetime.now()
    ).order_by('-publish_date')
    paginator = Paginator(records_list, settings.LIVE_ENGINE_NEWS_PER_PAGE)

    try:
        records = paginator.page(page)
    except EmptyPage:
        raise Http404

    return {
        'records': records,
        'category': category,
        'pagination_rule': '/%s/page/1/' % category.url
    }


@render('news/category.html')
def main(request, page=1):


    categories_id = Category.objects.all().values_list('pk', flat=True)

    records_list = Record.objects.select_related().filter(
        status = Record.STATUS_PUBLISHED,
        publish_date__lte = datetime.now(),
        category__in = categories_id
    ).order_by('-publish_date')
    paginator = Paginator(records_list, settings.LIVE_ENGINE_NEWS_PER_PAGE)

    try:
        records = paginator.page(page)
    except EmptyPage:
        raise Http404

    return {
        'records': records,
        'pagination_rule': '/page/1/',
        'is_main': True
    }